package com.qyhl.party.party.test.challenge;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.text.style.TextAppearanceSpan;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.party.R;
import com.qyhl.party.R2;
import com.qyhl.party.party.test.challenge.blank.QuestionBlankView;
import com.qyhl.question.question.challenge.count.QuestionCountDownView;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.SpanUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.dialog.CommonDialog;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.party.PartyChallengeBean;
import com.qyhl.webtv.commonlib.entity.party.PartyLoginBean;
import com.qyhl.webtv.commonlib.entity.party.PartyQuestionOptionBean;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


@SuppressLint("NonConstantResourceId")
@Route(path = ARouterPathConstant.PARTY_TEST_CHALLENGE)
public class PartyChallengeActivity extends BaseActivity implements PartyChallengeContract.PartyChallengeView {
    @BindView(R2.id.header)
    RoundedImageView header;
    @BindView(R2.id.name)
    TextView name;
    @BindView(R2.id.best_result)
    TextView bestResult;
    @BindView(R2.id.correct_combo)
    TextView correctCombo;
    @BindView(R2.id.blank_view)
    QuestionBlankView blankView;
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.option_recycler)
    RecyclerView optionRecycler;
    @BindView(R2.id.countDown_view)
    QuestionCountDownView countDownView;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.status)
    ImageView status;
    @BindView(R2.id.question_type)
    TextView questionType;
    @BindView(R2.id.commit_btn)
    Button commitBtn;
    @BindView(R2.id.correct_layout)
    LinearLayout correctLayout;
    @BindView(R2.id.correct_answer)
    TextView correctAnswer;
    @BindView(R2.id.content_layout)
    RelativeLayout contentLayout;
    @BindView(R2.id.finish_header_bg)
    ImageView finishHeaderBg;
    @BindView(R2.id.finish_header)
    RoundedImageView finishHeader;
    @BindView(R2.id.finish_name)
    TextView finishName;
    @BindView(R2.id.finish_best)
    TextView finishBest;
    @BindView(R2.id.finish_layout)
    LinearLayout finishLayout;
    @BindView(R2.id.finish_continue_btn)
    Button continueBtn;

    @Autowired(name = "token")
    String token;
    @Autowired(name = "realName")
    String realName;
    @Autowired(name = "bestCount")
    int bestCount;


    private List<PartyQuestionOptionBean> mDataList = new ArrayList<>();

    private CommonAdapter<PartyQuestionOptionBean> commonAdapter;

    private PartyChallengePresenter mPresenter;

    private PartyChallengeBean mBean;

    private String mAnswer;

    private String correct;

    private int mUseTime = 1;

    private boolean isIdle = true;//是否空闲

    private final static int MAX_TIME = 60;

    private int currentType;

    private boolean isRepeat = false;

    private enum ErrorStatus {
        START_ERROR,
        TIMEOUT_ERROR,
        SUBMIT_ERROR
    }

    private ErrorStatus mErrorStatus = ErrorStatus.START_ERROR;

    @Override
    protected int getContentView() {
        return R.layout.party_activity_challenge;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.statusBarColor(R.color.party_status_bar).statusBarDarkFont(false).fitsSystemWindows(true).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);
        mPresenter = new PartyChallengePresenter(this);

        countDownView.setTotalTime(MAX_TIME * 1000);
        countDownView.setText(MAX_TIME + "s");

        String headIcon = CommonUtils.getInstance().getUserLogo();
        Glide.with(PartyChallengeActivity.this)
                .load(headIcon)
                .apply(new RequestOptions().placeholder(R.drawable.comment_head_default).error(R.drawable.comment_head_default))
                .into(header);

        Glide.with(PartyChallengeActivity.this)
                .load(headIcon)
                .apply(new RequestOptions().placeholder(R.drawable.comment_head_default).error(R.drawable.comment_head_default))
                .into(finishHeader);

        name.setText(realName);
        finishName.setText(realName);

        bestResult.setText("最好成绩：" + bestCount + "道");

        optionRecycler.setLayoutManager(new LinearLayoutManager(this));
        optionRecycler.setAdapter(commonAdapter = new CommonAdapter<PartyQuestionOptionBean>(this, R.layout.question_item_option, mDataList) {
            @Override
            public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
                if (payloads.size() < 1) {
                    onBindViewHolder(holder, position);
                } else {
                    TextView optionTxt = holder.getView(R.id.option_txt);

                    if (currentType == 1) {
                        switch (mDataList.get(position).getStatus()) {
                            case 0://普通
                                holder.setTextColorRes(R.id.option, R.color.global_black_lv1);
                                holder.setTextColorRes(R.id.option_txt, R.color.global_black_lv1);
                                holder.setBackgroundRes(R.id.option_layout, R.drawable.question_normal_bg);
                                optionTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                                break;
                            case 1://正确
                                holder.setTextColorRes(R.id.option, R.color.question_option_correct);
                                holder.setTextColorRes(R.id.option_txt, R.color.question_option_correct);
                                holder.setBackgroundRes(R.id.option_layout, R.drawable.question_correct_bg);
                                optionTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(PartyChallengeActivity.this, R.drawable.question_option_correct_icon), null);
                                break;
                            case 2://错误
                                holder.setTextColorRes(R.id.option, R.color.question_option_error);
                                holder.setTextColorRes(R.id.option_txt, R.color.question_option_error);
                                holder.setBackgroundRes(R.id.option_layout, R.drawable.question_error_bg);
                                optionTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(PartyChallengeActivity.this, R.drawable.question_option_error_icon), null);
                                break;
                        }
                    } else {
                        if (mDataList.get(position).isChecked()) {
                            holder.setTextColorRes(R.id.option, R.color.global_black_lv1);
                            holder.setTextColorRes(R.id.option_txt, R.color.global_black_lv1);
                            holder.setBackgroundRes(R.id.option_layout, R.drawable.question_selected_bg);
                            optionTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                        } else {
                            holder.setTextColorRes(R.id.option, R.color.global_black_lv1);
                            holder.setTextColorRes(R.id.option_txt, R.color.global_black_lv1);
                            holder.setBackgroundRes(R.id.option_layout, R.drawable.question_normal_bg);
                            optionTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                        }
                    }
                }

            }

            @Override
            protected void convert(ViewHolder holder, PartyQuestionOptionBean bean, int position) {
                TextView optionTxt = holder.getView(R.id.option_txt);

                if (currentType == 1) {
                    switch (bean.getStatus()) {
                        case 0://普通
                            holder.setTextColorRes(R.id.option, R.color.global_black_lv1);
                            holder.setTextColorRes(R.id.option_txt, R.color.global_black_lv1);
                            holder.setBackgroundRes(R.id.option_layout, R.drawable.question_normal_bg);
                            optionTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                            break;
                        case 1://正确
                            holder.setTextColorRes(R.id.option, R.color.question_option_correct);
                            holder.setTextColorRes(R.id.option_txt, R.color.question_option_correct);
                            holder.setBackgroundRes(R.id.option_layout, R.drawable.question_correct_bg);
                            optionTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(PartyChallengeActivity.this, R.drawable.question_option_correct_icon), null);
                            break;
                        case 2://错误
                            holder.setTextColorRes(R.id.option, R.color.question_option_error);
                            holder.setTextColorRes(R.id.option_txt, R.color.question_option_error);
                            holder.setBackgroundRes(R.id.option_layout, R.drawable.question_error_bg);
                            optionTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(PartyChallengeActivity.this, R.drawable.question_option_error_icon), null);
                            break;
                    }
                } else {
                    if (bean.isChecked()) {
                        holder.setTextColorRes(R.id.option, R.color.global_black_lv1);
                        holder.setTextColorRes(R.id.option_txt, R.color.global_black_lv1);
                        holder.setBackgroundRes(R.id.option_layout, R.drawable.question_selected_bg);
                        optionTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                    } else {
                        holder.setTextColorRes(R.id.option, R.color.global_black_lv1);
                        holder.setTextColorRes(R.id.option_txt, R.color.global_black_lv1);
                        holder.setBackgroundRes(R.id.option_layout, R.drawable.question_normal_bg);
                        optionTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                    }
                }
                holder.setText(R.id.option_txt, bean.getContent());

                holder.setText(R.id.option, bean.getOption() + ".");
            }
        });


        mPresenter.startChallenge(token);
    }

    @Override
    protected void setListener() {
        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                switch (currentType) {
                    case 1:
                        if (isIdle) {
                            isIdle = false;
                            mAnswer = mDataList.get(position).getOption();
                            mUseTime = countDownView.getmCountingVal() / 1000;
                            mPresenter.submit(token, mBean.getRecordId(), mBean.getQuestion().getId(), mAnswer, mUseTime == 0 ? 1 : mUseTime);
                        }
                        break;
                    case 2:
                        mDataList.get(position).setChecked(!mDataList.get(position).isChecked());
                        commonAdapter.notifyDataSetChanged();
                        break;
                    case 3:
                        break;
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                switch (mErrorStatus) {
                    case START_ERROR:
                        mPresenter.startChallenge(token);
                        break;
                    case TIMEOUT_ERROR:
                        mPresenter.submitTimeOut(token, mBean.getRecordId());
                        break;
                    case SUBMIT_ERROR:
                        mPresenter.submit(token, mBean.getRecordId(), mBean.getQuestion().getId(), mAnswer, mUseTime == 0 ? 1 : mUseTime);
                        break;
                }
            }
        });

        countDownView.setOnCountClickListener(new QuestionCountDownView.onCountClickListener() {
            @Override
            public void onCountingClick() {

            }

            @Override
            public void onCountOverClick() {
                mPresenter.submitTimeOut(token, mBean.getRecordId());
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onCountTickListener(final int second) {
                PartyChallengeActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        countDownView.setText(second + "s");
                    }
                });
            }
        });
    }

    @OnClick({R2.id.finish_btn, R2.id.commit_btn, R2.id.finish_continue_btn})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.commit_btn) {
            switch (currentType) {
                case 1:
                    break;
                case 2:
                    if (isIdle) {
                        isIdle = false;
                        StringBuilder str = new StringBuilder();
                        for (int i = 0; i < mDataList.size(); i++) {
                            if (mDataList.get(i).isChecked()) {
                                str.append(mDataList.get(i).getOption());
                            }
                        }
                        if (str.length() > 0) {
                            mAnswer = str.toString();
                            mUseTime = countDownView.getmCountingVal() / 1000;
                            mPresenter.submit(token, mBean.getRecordId(), mBean.getQuestion().getId(), mAnswer, mUseTime == 0 ? 1 : mUseTime);
                        } else {
                            isIdle = true;
                            showToast("请做出选择！");
                        }
                    }
                    break;
                case 3:
                    if (StringUtils.isNotEmpty(blankView.getData())) {
                        if (isIdle) {
                            isIdle = false;
                            mAnswer = blankView.getData();
                            mUseTime = countDownView.getmCountingVal() / 1000;
                            mPresenter.submit(token, mBean.getRecordId(), mBean.getQuestion().getId(), mAnswer, mUseTime == 0 ? 1 : mUseTime);
                        }
                    } else {
                        showToast("请填写答案！");
                    }
                    break;
            }
        } else if (id == R.id.finish_btn) {
            finish();
        } else if (id == R.id.finish_continue_btn) {
            finishLayout.setVisibility(View.GONE);
            contentLayout.setVisibility(View.VISIBLE);
            isRepeat = true;
            mPresenter.startChallenge(token);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void startSuccess(PartyChallengeBean bean) {
        correctLayout.setVisibility(View.GONE);
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        countDownView.setVisibility(View.VISIBLE);
        status.setVisibility(View.GONE);

        mBean = bean;

        correct = bean.getQuestion().getCorrectAnswer();
        if (isRepeat) {
            countDownView.setText(MAX_TIME + "s");
            countDownView.restart();
        } else {
            countDownView.startTimer();
        }

        title.setText(bean.getQuestion().getContent());
        SpanUtils spanUtils = new SpanUtils(this);
        spanUtils.append("您已连续答对").setSpans(new TextAppearanceSpan(this, R.style.question_tips_normal))
                .append("0").setSpans(new TextAppearanceSpan(this, R.style.question_tips_important))
                .append("题").setSpans(new TextAppearanceSpan(this, R.style.question_tips_normal));
        correctCombo.setText(spanUtils.create());

        currentType = mBean.getQuestion().getType();

        switch (currentType) {
            case 1:
                commitBtn.setVisibility(View.GONE);
                blankView.setVisibility(View.GONE);
                title.setVisibility(View.VISIBLE);
                optionRecycler.setVisibility(View.VISIBLE);
                questionType.setText("单选题");

                mDataList.clear();
                mDataList.addAll(bean.getQuestion().getOptionList());
                commonAdapter.notifyDataSetChanged();
                break;
            case 2:
                commitBtn.setVisibility(View.VISIBLE);
                optionRecycler.setVisibility(View.VISIBLE);
                title.setVisibility(View.VISIBLE);
                blankView.setVisibility(View.GONE);
                questionType.setText("多选题");

                mDataList.clear();
                mDataList.addAll(bean.getQuestion().getOptionList());
                commonAdapter.notifyDataSetChanged();
                break;
            case 3:
                optionRecycler.setVisibility(View.GONE);
                commitBtn.setVisibility(View.VISIBLE);
                title.setVisibility(View.GONE);
                blankView.setVisibility(View.VISIBLE);
                questionType.setText("填空题");

                String title = mBean.getQuestion().getContent();
                title = title.replaceAll("[(][+]", "+");
                title = title.replaceAll("[+][)]", "+");
                mBean.getQuestion().setContent(title);

                blankView.setQuestion(mBean.getQuestion());
                break;
        }
    }

    @Override
    public void startError(String msg) {
        if (msg.contains("403")) {
            mPresenter.isLogin();
            loadMask.setErrorText("登录授权失败，请重新授权！");
        } else {
            loadMask.setErrorText(msg);
        }
        correctLayout.setVisibility(View.GONE);
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        mErrorStatus = ErrorStatus.START_ERROR;
    }

    @Override
    public void submitSuccess(PartyChallengeBean bean) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        correctLayout.setVisibility(View.GONE);
        contentLayout.setVisibility(View.VISIBLE);
        finishLayout.setVisibility(View.GONE);
        countDownView.cancle();

        if (bean.isCorrect()) {//是否正确
            mBean = bean;
            for (int i = 0; i < mDataList.size(); i++) {
                if (mDataList.get(i).getOption().equals(mAnswer)) {
                    mDataList.get(i).setStatus(1);
                    commonAdapter.notifyItemChanged(i, "option");
                }
            }
            countDownView.setVisibility(View.GONE);
            status.setImageResource(R.drawable.question_status_correct_icon);
            status.setVisibility(View.VISIBLE);

            final SpanUtils spanUtils = new SpanUtils(this);
            spanUtils.append("您已连续答对").setSpans(new TextAppearanceSpan(this, R.style.question_tips_normal))
                    .append(bean.getCorrectCount() + "").setSpans(new TextAppearanceSpan(this, R.style.question_tips_important))
                    .append("题").setSpans(new TextAppearanceSpan(this, R.style.question_tips_normal));

            if (bean.isOver()) {//是否结束
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isIdle = true;
                        contentLayout.setVisibility(View.GONE);
                        finishLayout.setVisibility(View.VISIBLE);
                        finishHeaderBg.setBackgroundResource(R.drawable.party_challenge_header_success_bg);
                        finishBest.setText("最好成绩：" + mBean.getCorrectCount() + "道");
                        continueBtn.setText("继续挑战");
                    }
                }, 2800);
            } else {

                new Handler().postDelayed(new Runnable() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void run() {
                        currentType = mBean.getQuestion().getType();
                        correct = mBean.getQuestion().getCorrectAnswer();
                        isIdle = true;
                        correctCombo.setText(spanUtils.create());
                        title.setText(mBean.getQuestion().getContent());

                        switch (currentType) {
                            case 1:
                                commitBtn.setVisibility(View.GONE);
                                blankView.setVisibility(View.GONE);
                                title.setVisibility(View.VISIBLE);
                                optionRecycler.setVisibility(View.VISIBLE);
                                questionType.setText("单选题");

                                mDataList.clear();
                                mDataList.addAll(bean.getQuestion().getOptionList());
                                commonAdapter.notifyDataSetChanged();
                                break;
                            case 2:
                                commitBtn.setVisibility(View.VISIBLE);
                                optionRecycler.setVisibility(View.VISIBLE);
                                title.setVisibility(View.VISIBLE);
                                blankView.setVisibility(View.GONE);
                                questionType.setText("多选题");

                                mDataList.clear();
                                mDataList.addAll(bean.getQuestion().getOptionList());
                                commonAdapter.notifyDataSetChanged();
                                break;
                            case 3:
                                optionRecycler.setVisibility(View.GONE);
                                commitBtn.setVisibility(View.VISIBLE);
                                title.setVisibility(View.GONE);
                                blankView.setVisibility(View.VISIBLE);
                                questionType.setText("填空题");

                                String title = mBean.getQuestion().getContent();
                                title = title.replaceAll("[(][+]", "+");
                                title = title.replaceAll("[+][)]", "+");
                                mBean.getQuestion().setContent(title);

                                blankView.setQuestion(mBean.getQuestion());
                                break;
                        }

                        countDownView.setVisibility(View.VISIBLE);
                        status.setVisibility(View.GONE);
                        countDownView.setText(MAX_TIME + "s");
                        countDownView.restart();

                        commonAdapter.notifyDataSetChanged();
                    }
                }, 1500);
            }
        } else {//错误
            currentType = mBean.getQuestion().getType();
            countDownView.setVisibility(View.GONE);
            status.setImageResource(R.drawable.question_status_error_icon);
            status.setVisibility(View.VISIBLE);

            if (currentType == 1) {
                for (int i = 0; i < mDataList.size(); i++) {
                    if (mDataList.get(i).getOption().equals(mAnswer)) {
                        mDataList.get(i).setStatus(2);
                    }

                    if (mDataList.get(i).getOption().equals(mBean.getQuestion().getCorrectAnswer())) {
                        mDataList.get(i).setStatus(1);
                    }
                }
                commonAdapter.notifyDataSetChanged();
            } else {
                correctLayout.setVisibility(View.VISIBLE);
                correctAnswer.setText(correct);
            }

            new Handler().postDelayed(new Runnable() {
                @SuppressLint("SetTextI18n")
                @Override
                public void run() {
                    contentLayout.setVisibility(View.GONE);
                    finishLayout.setVisibility(View.VISIBLE);
                    finishHeaderBg.setBackgroundResource(R.drawable.party_challenge_header_fail_bg);
                    finishBest.setText("最好成绩：" + mBean.getCorrectCount() + "道");
                    continueBtn.setText("重新开始");
                    isIdle = true;

                }
            }, 1500);
        }
    }

    @Override
    public void submitError(String msg) {
        if (msg.contains("403")) {
            mPresenter.isLogin();
            loadMask.setErrorText("登录授权失败，请重新授权！");
        } else {
            loadMask.setErrorText(msg);
        }
        isIdle = true;
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Error);
        mErrorStatus = ErrorStatus.SUBMIT_ERROR;
        countDownView.cancle();
    }

    @Override
    public void timeOutSuccess(PartyChallengeBean bean) {
        isIdle = true;
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        countDownView.cancle();
        if (currentType == 1) {
            for (int i = 0; i < mDataList.size(); i++) {
                if (mDataList.get(i).getOption().equals(mAnswer)) {
                    mDataList.get(i).setStatus(2);
                    commonAdapter.notifyDataSetChanged();
                }
            }
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                contentLayout.setVisibility(View.GONE);
                finishLayout.setVisibility(View.VISIBLE);
                finishHeaderBg.setBackgroundResource(R.drawable.party_challenge_header_fail_bg);
                finishBest.setText("最好成绩：" + mBean.getCorrectCount() + "道");
                continueBtn.setText("重新开始");

            }
        }, 1500);
    }

    @Override
    public void timeOutError(String msg) {
        if (msg.contains("403")) {
            mPresenter.isLogin();
            loadMask.setErrorText("登录授权失败，请重新授权！");
        } else {
            loadMask.setErrorText(msg);
        }
        isIdle = true;
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Error);
        mErrorStatus = ErrorStatus.TIMEOUT_ERROR;
        countDownView.cancle();
    }

    @Override
    public void LoginSuccess(PartyLoginBean bean) {
        isIdle = true;
        token = bean.getAuthorization();
    }

    @Override
    public void LoginError(String msg) {
        isIdle = true;
        showToast(msg);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new CommonDialog.Builder(this)
                    .setCancelable(true)
                    .setCanceledOnTouchOutside(false)
                    .setTitle("提示")
                    .setMessage("是否要退出当前答题挑战？")
                    .setPositiveButton("退出", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    }).setNegativeButton("取消", new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            }).show();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        countDownView.cancle();
    }
}
